module(..., package.seeall)

--====================================================================--
-- POP UP: MYSTATS POPUP
--====================================================================--

--[[

******************
 - INFORMATION
******************

  - Sample scene.

--]]

--import the scrolling classes
local scrollView = require("scrollView")
local util       = require("util")

local imgPath = "images/"

local widget = require "widget"
local store = require("store")

new = function ()
	
	------------------
	-- Groups
	------------------
	
	local localGroup = display.newGroup()
	
	------------------
	-- Display Objects
	------------------
	
	local w, h = display.contentWidth, display.contentHeight
	local background = display.newImage( "background3.png" )
	background.xScale = 0.94
	background.yScale = 0.8
	background.alpha = 0.95
	
	--close after purchase token success
	local function closeAfterPurchase(event)
		director:closePopUp()
	end
	
	--close button
	local btBatsut = function ( event )
		if event.phase == "release" then
			director:closePopUp()
		end
	end

	local btBatsu = ui.newButton{
					default = imgPath .. "cross_yellow_64.png",
					over = imgPath .. "cross_green_64.png",
					onEvent = btBatsut,
					id = "btBatsu"
	}
	btBatsu.x, btBatsu.y = 295,20

	--eo close button
	
	--popup header
	local headerBdr = display.newRect( 10, 0, display.contentWidth-20, 30 )
	headerBdr.strokeWidth = 1
	headerBdr:setFillColor(90, 90, 90)
	headerBdr:setStrokeColor(70, 70, 70)
	
	--popup title
	local popupTitle = display.newText( "Get Token", 120, 5, native.systemFontBold, 20 )

	--buttons
	local function onButtonEvent( event )
		local btn = event.target
		if event.phase == "press" then
			print( "User has began pressing the button with id: " .. btn.id )
		elseif event.phase == "moved" then
			print( "User has dragged their finger while pressing the button." )
		elseif event.phase == "release" then
			print( "User has lifted finger from button." )        
		end
	end
	
	--btn get 1 token
	local function onButton10Event( event )
		local btn = event.target
		if event.phase == "press" then
			print( "User has began pressing the button 10 with id: " .. btn.id )
			print("Ka-ching! Purchasing " .. tostring(productId))
			store.purchase( {"android.test.purchased"} )
			--store.purchase( {"unmanaged_token_10"} )

		elseif event.phase == "moved" then
			print( "User has dragged their finger while pressing the button 10." )
		elseif event.phase == "release" then
			print( "User has lifted finger from button 10." )        
		end
	end
	local button = widget.newButton{
		default = imgPath .. "btn_bg.png",
		over = imgPath .. "btn_black.png",
		label = "Buy 10 Token - JPY100",
		font = "Helvetica-Bold",
		fontSize = 16,
		emboss = true,
		onEvent = onButton10Event
	}
	button.x = display.contentCenterX
	button.y = 80
	button.xScale = 0.9
	button.yScale = 0.9

	--btn get 20 token
	local function onButton20Event( event )
		local btn = event.target
		if event.phase == "press" then
			print( "User has began pressing the button 10 with id: " .. btn.id )
			print("Ka-ching! Purchasing " .. tostring(productId))
			--store.purchase( {"android.test.cancelled"} )
			store.purchase( {"unmanaged_token_50"} )

		elseif event.phase == "moved" then
			print( "User has dragged their finger while pressing the button 10." )
		elseif event.phase == "release" then
			print( "User has lifted finger from button 10." )        
		end
	end
	local button20 = widget.newButton{
		default = imgPath .. "btn_bg.png",
		over = imgPath .. "btn_black.png",
		label = "Buy 50 Potion - JPY200",
		font = "Helvetica-Bold",
		fontSize = 16,
		emboss = true,
		onEvent = onButton20Event
	}
	button20.x = display.contentCenterX
	button20.y = 130
	button20.xScale = 0.9
	button20.yScale = 0.9

	--btn get 100 token
	local function onButton100Event( event )
		local btn = event.target
		if event.phase == "press" then
			print( "User has began pressing the button 10 with id: " .. btn.id )
			print("Ka-ching! Purchasing " .. tostring(productId))
			--store.purchase( {"android.test.item_unavailable"} )
			store.purchase( {"unmanaged_token_100"} )
			--masashi.swd@gmail.com

		elseif event.phase == "moved" then
			print( "User has dragged their finger while pressing the button 10." )
		elseif event.phase == "release" then
			print( "User has lifted finger from button 10." )        
		end
	end
	local button100 = widget.newButton{
		default = imgPath .. "btn_bg.png",
		over = imgPath .. "btn_black.png",
		label = "Buy 100 Potion - JPY300",
		font = "Helvetica-Bold",
		fontSize = 16,
		emboss = true,
		onEvent = onButton100Event
	}
	button100.x = display.contentCenterX
	button100.y = 180
	button100.xScale = 0.9
	button100.yScale = 0.9

	
	--popup window group
	local headerGroup = display.newGroup();
	headerGroup.x = 0;
	headerGroup.y = 50;
	headerGroup:insert(headerBdr)
	headerGroup:insert(popupTitle)
	headerGroup:insert(btBatsu)
	headerGroup:insert(button)
	headerGroup:insert(button20)
	headerGroup:insert(button100)
	headerGroup.xScale = 1;
	headerGroup.yScale = 1;			
	--eo popup window group

	--tirana
	local potion = display.newImage( imgPath .. "potion_128.png" )
	potion.xScale = 2.0
	potion.yScale = 2.0
	potion.x = 250
	potion.y = 400
	---potion.rotation = -70
	potion.rotation = 0
	--tirana.xScale = -0.4


	------------------
	-- Listeners
	------------------
	
	local touched = function ( event )
		if event.phase == "ended" then
			director:closePopUp()
		end
	end
	
	local closed = function ( event )
		if event.phase == "ended" then
			director:closePopUp()
		end
	end
	
	--====================================================================--
	-- IN-APP-BILLING
	--====================================================================--
	-- Product IDs for the "google" Android Marketplace store.
	local googleProductList =
	{
		-- These product IDs are used for testing and is supported by all Android apps.
		-- Purchasing these products will not bill your account.
		"android.test.purchased",			-- Marketplace will always successfully purchase this product ID.
		"android.test.canceled",			-- Marketplace will always cancel a purchase of this product ID.
		"android.test.item_unavailable",	-- Marketplace will always indicate this product ID as unavailable.
	}

	local function transactionCallback( event )
		local transaction = event.transaction
		
		print("ID: " .. transaction.productIdentifier)
		 
		if transaction.state == "purchased" then
		-- Transaction was successful; unlock/download content now
			popupTitle.text = "Purchase success : " .. transaction.productIdentifier
			--native.showAlert("berjaya beli");
			--update token in db
			local q = [[UPDATE data SET coin_count=10 WHERE _id=1;]]
			db:exec( q )

			local alert = native.showAlert( "Token purchased succcessfully", "Now you have ++ Tokens", { "YES" }, closeAfterPurchase)						

		 
		elseif transaction.state == "restored" then
		-- You'll never reach this transaction state on Android.
		 
		 
		elseif transaction.state == "refunded" then
		-- Android-only; user refunded their purchase
		local productId = transaction.productIdentifier
		 
		-- Restrict/remove content associated with above productId now
		 
		elseif transaction.state == "cancelled" then
		-- Transaction was cancelled; tell you app to react accordingly here
			popupTitle.text = "cancelled"
		 
		elseif transaction.state == "failed" then
		-- Transaction failed; tell you app to react accordingly here
			popupTitle.text = "Purchase failed : " .. transaction.productIdentifier
		end
		 
		-- The following must be called after transaction is complete.
		-- If your In-app product needs to download, do not call the following
		-- function until AFTER the download is complete:
		 
		store.finishTransaction( transaction )
	end
	 
	--store.init( "google", transactionCallback )	
	
	--====================================================================--
	-- INITIALIZE
	--====================================================================--
	
	local function initVars ()
		
		------------------
		-- Inserts
		------------------
		
		localGroup:insert( background )
		localGroup:insert( potion )
		localGroup:insert( headerGroup )
		--localGroup:insert( scroller )
		--localGroup:insert( btBatsu )
		store.init( "google", transactionCallback )	
		print("Using Google's Android In-App Billing system.")
		
		
		------------------
		-- Positions
		------------------
		
		--
		
		------------------
		-- Colors
		------------------
		
		--title:setTextColor( 255,255,255 )
		
		------------------
		-- Listeners
		------------------
		
		--background:addEventListener( "touch" , touched )
		
	end
	
	------------------
	-- Initiate variables
	------------------
	
	initVars()
	
	------------------
	-- MUST return a display.newGroup()
	------------------
	
	return localGroup
	
end
